<?php

define('IN_DOUCO', true);
require (dirname(__FILE__) . '/include/init.php');
// rec操作项的初始化
$rec = $check->is_rec($_REQUEST['rec']) ? $_REQUEST['rec'] : 'default';

$res = $dou->get_all("SELECT * FROM ".$dou->table('chatroom')." ORDER BY id DESC");
foreach($res as $k=>$v) $rooms[$v['roomnumber']] = $v;
$smarty->assign('rooms', $rooms);
$smarty->assign('rec', $rec);
$smarty->assign('cur', 'chatlog');
$smarty->assign('ur_here', $_LANG['chatlog']);
$smarty->assign('chatlog_link', array ('text' => $_LANG['chatlog'],'href' => 'chatlog.php')); 
/**
 * +----------------------------------------------------------
 * 会员列表
 * +----------------------------------------------------------
 */
if ($rec == 'default') {
    $username = i('username','','request');
    $content  = i('content','','request');
    $room_id  = i('room_id','','request');
    $remark   = i('remark','','request');
    $status   = i('status','','request');
    $msg_status   = i('msg_status','','request');
    $where = " WHERE 1 ";
    if($username){
        $where .= " AND u.username LIKE '%$username%' ";
        $get = "&username=$username";
    }
    if($content){
        $where .= " AND c.content LIKE '%$content%' ";
        $get = "&content=$content";
    }
    if($room_id){
        $where .= " AND c.room_id = '$room_id' ";
        $get = "&room_id=$room_id";
    }
    if($remark != ''){
        $where .= " AND c.remark = '$remark' ";
        $get = "&remark=$remark";
    }
    if($status != ''){
        $where .= " AND c.status = '$status' ";
        $get = "&status=$status";
    }
    if($msg_status != ''){
        if($msg_status == 'remark')    {$where .= " AND c.remark = '1' ";$get = "&msg_status=remark";}
        if($msg_status == 'blacklist') {$where .= " AND c.status = 'hidden' ";$get = "&msg_status=blacklist";}
    }
    //分页
    $page = $check->is_number($_REQUEST['page']) ? $_REQUEST['page'] : 1;
    $page_url = 'chatlog.php?1=1';
    $sql = "SELECT c.*,u.username,u.mobile,u.group_id,u.isgag,u.status user_status FROM " . $dou->table('chatlog') . "c ".
        "LEFT JOIN ".$dou->table('user')." u ON c.user_id=u.id ".
        "$where ORDER BY id DESC";
    $limit = $dou->pager_sql($sql, 25, $page, $page_url, $get);
    $res = $dou->get_all($sql.$limit);
    foreach ($res as $k=>$row) {
        $chatlog_list[$k] = $row;
        //$chatlog_list[$k]['content']   = htmlspecialchars_decode($row['content']);//strip_tags()
        $chatlog_list[$k]['content']   = preg_replace('#src="/(.*)"#', "src=\"{$img_url}$1\"", htmlspecialchars_decode($row['content']));
        $chatlog_list[$k]['chat_time'] = date("Y-m-d H:i:s", $row['chat_time']);
    }
    // 赋值给模板
    $smarty->assign('chatlog_list', $chatlog_list);
    $smarty->display('chatlog.htm');
}
//编辑
elseif ($rec == 'edit') {
    $smarty->assign('ur_here', $_LANG['chatlog']);
    $smarty->assign('action_link', array ('text' => $_LANG['chatlog'],'href' => 'chatlog.php' ));
    // 验证并获取合法的ID
    $id = $check->is_number($_REQUEST['id']) ? $_REQUEST['id'] : '';
    $chatlog = $dou->get_row("SELECT c.*,u.username,u.nickname,u.mobile FROM ".$dou->table('chatlog')."c ".
        "LEFT JOIN ".$dou->table('user')."u ON c.user_id=u.id WHERE c.id='$id' ");
    $chatlog['content'] = htmlspecialchars_decode($chatlog['content']);
    // CSRF防御令牌生成
    $smarty->assign('token', $firewall->get_token());
    $smarty->assign('chatlog', $chatlog);
    $smarty->display('chatlog.htm');
}
elseif ($rec == 'update') {
    // CSRF防御令牌验证
    $firewall->check_token($_POST['token']);
    $sql = $dou->update_sql('chatlog', array('status'=>$_POST['status']), "id='{$_POST['id']}'");
    $dou->query($sql);
    //操作记录
    $log_msg = $_LANG['user_edit'].': '.$_POST['username'];
    $dou->create_admin_log($log_msg);
    //$dou->dou_msg($_LANG['edit_succes'], 'chatlog.php');
    header('Location:chatlog.php');
} 

//删除
elseif ($rec == 'del') {
    // 验证并获取合法的ID
    $id = $check->is_number($_REQUEST['id']) ? $_REQUEST['id'] : $dou->dou_msg($_LANG['illegal'], 'chatlog.php');
    $content = $dou->get_one("SELECT content FROM " . $dou->table('chatlog') . " WHERE id = '$id'");
    if (isset($_POST['confirm']) ? $_POST['confirm'] : '') {
        $dou->create_admin_log($_LANG['user_del'] . ': ' . $content);
        $dou->delete($dou->table('chatlog'), "id = $id", 'chatlog.php');
    } else {
        $_LANG['del_check'] = preg_replace('/d%/Ums', $content, $_LANG['del_check']);
        $dou->dou_msg($_LANG['del_check'], 'chatlog.php', '', '30', "chatlog.php?rec=del&id=$id");
    }
}

//批量操作选择
elseif ($rec == 'action') {
    if (is_array($_REQUEST['checkbox']) || $_REQUEST['checkbox']>0) {
        $_REQUEST['checkbox'] = is_string($_REQUEST['checkbox']) ? explode(',', $_REQUEST['checkbox']) : $_REQUEST['checkbox'];
        //禁言用户
        if ($_REQUEST['opt'] == 'add_gag' || $_REQUEST['opt'] == 'del_gag' || isset($_REQUEST['opt_add_gag']) ) {
            $isgag = $_REQUEST['opt']=='del_gag' ? '0':'1';
            $users = $dou->get_column( "SELECT user_id FROM ".$dou->table('chatlog')." WHERE 1 AND ".$dou->where_in($_REQUEST['checkbox'],'id')."GROUP BY user_id", 'user_id' );
            $dou->update_all('user', $users, 'id', "isgag='{$isgag}'");
            if($isgag == 1) $dou->update_all('user', $users, 'id', "gag_time=gag_time+1");
        }
        //拉黑用户
        if ($_REQUEST['opt'] == 'add_blacklist' || $_REQUEST['opt'] == 'del_blacklist' || isset($_REQUEST['opt_add_blacklist']) ) {
            $status = $_REQUEST['opt']=='del_blacklist' ? 'normal':'hidden';
            $users = $dou->get_column( "SELECT user_id FROM ".$dou->table('chatlog')." WHERE 1 AND ".$dou->where_in($_REQUEST['checkbox'],'id')."GROUP BY user_id", 'user_id' );
            $dou->update_all('user', $users, 'id', "status='{$status}'"); //屏蔽用户
            $dou->update_all('chatlog', $users, 'user_id', "status='{$status}'"); //屏蔽用户关联消息
        }
        //屏蔽消息
        if ($_REQUEST['opt'] == 'add_hidden' || $_REQUEST['opt'] == 'del_hidden' || isset($_REQUEST['opt_add_hidden']) ) {
            $status = $_REQUEST['opt']=='del_hidden' ? 'normal' : 'hidden';
            $dou->update_all('chatlog', $_REQUEST['checkbox'], 'id', "status='{$status}'");
            //推送删除消息
            if($status == 'hidden'){
                $msgs = $dou->get_column("SELECT msg_id FROM".$dou->table('chatlog')."WHERE ".$dou->where_in($_REQUEST['checkbox'], 'id'), 'msg_id');
                foreach($msgs as $k=>$v) push_hiddenMsg(array('msg_id'=>$v));
            }
            
        }
        //删除聊天记录
        if ($_REQUEST['opt'] == 'del_all' || isset($_REQUEST['opt_del_all']) ) {//批量删除
            $dou->delete_all('chatlog', $_REQUEST['checkbox'], 'id');
        }
        header('Location:'.$_SERVER["HTTP_REFERER"]);
    } else {
        $dou->dou_msg($_LANG['record_select_empty']);
    }

}

?>